﻿using System.Collections.Generic;

namespace TemplateFoundation.Feeds
{
    /// <summary>
    /// Represents a list of RSS items for an RSS feed
    /// </summary>
    /// <seealso cref="RssChannel"/>
    public class RssItemCollection : ICollection<RssItem>
    {
        private List<RssItem> _items = new List<RssItem>();

        #region ICollection<RssItem> Members

        /// <summary>
        /// Adds an RSS item to the collection, maintaining a descending sort order by date
        /// </summary>
        /// <param name="item"></param>
        public void Add(RssItem item)
        {
            for(int i=0;i<_items.Count;i++)
            {
                if(_items[i].Date<item.Date)
                {
                    _items.Insert(i,item);
                    return;
                }
            }

            _items.Add(item);
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            _items.Clear();
        }

        /// <summary>
        /// Determines whether specified RSS item is part of the collection
        /// </summary>
        /// <param name="item"></param>
        public bool Contains(RssItem item)
        {
            return _items.Contains(item);
        }

        /// <summary>
        /// Copies to.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="arrayIndex">Index of the array.</param>
        public void CopyTo(RssItem[] array, int arrayIndex)
        {
            _items.CopyTo(array,arrayIndex);
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count
        {
            get { return _items.Count; }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is read only.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is read only; otherwise, <c>false</c>.
        /// </value>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// Removes the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool Remove(RssItem item)
        {
            return _items.Remove(item);
        }

        #endregion

        #region IEnumerable<RssItem> Members

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<RssItem> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion
    }
}
